package com.easylinkin.linkappapi.openapi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.exceptions.ExceptionEnum;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.entity.emp.EmpUserBase;
import com.easylinkin.linkappapi.lobar.entity.emp.EmpUserBaseQuery;
import com.easylinkin.linkappapi.lobar.entity.vo.EmpUserBaseVO;
import com.easylinkin.linkappapi.lobar.mapper.EmpUserBaseMapper;
import com.easylinkin.linkappapi.openapi.service.EmpUserBaseService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class EmpUserBaseServiceImpl extends ServiceImpl<EmpUserBaseMapper, EmpUserBase> implements
    EmpUserBaseService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param empUserBase
   */
  @Override
  public void insert(EmpUserBase empUserBase) {
    /**
     * 验证重复
     */
    this.checkExist(empUserBase);
    //设置基本属性
    empUserBase.setEmployStatus(1);
    empUserBase.setEmployTime(new Date());
    this.setBase(empUserBase);
    this.save(empUserBase);
  }

  /**
   * 根据id编辑
   * @param empUserBase
   */
  @Override
  public void updateOne(EmpUserBase empUserBase) {
    /**
     * 验证重复
     */
    this.checkExist(empUserBase);
    //设置基本属性
    this.setBase(empUserBase);
    this.updateById(empUserBase);
  }

  @Override
  public EmpUserBaseVO getExistByQuery(EmpUserBaseQuery query) {
    EmpUserBaseVO data = baseMapper.getExistByQuery(query);
    if (ObjectUtil.isNull(data)) {
      throw new BusinessException(ExceptionEnum.CommonExceptionEnum.RECORD_NOT_EXISTS_ERROR, "抱歉，未查询到你的身份信息");
    }

    return data;
  }

  @Override
  public IPage<EmpUserBase> queryListByPage(RequestModel<EmpUserBase> requestModel) {
    Page page = requestModel.getPage();
    EmpUserBase empUserBase = requestModel.getCustomQueryParams();
    IPage<EmpUserBase> empUserBaseIPage = baseMapper.queryListByPage(page, empUserBase);
    return empUserBaseIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(EmpUserBase empUserBase) {
    QueryWrapper<EmpUserBase> queryWrapper = new QueryWrapper<>();
    //设置判断重复条件
    queryWrapper.eq("card_", empUserBase.getCard());
    //编辑的时候存在id
    Optional.ofNullable(empUserBase.getId()).ifPresent(id -> queryWrapper.ne("id",empUserBase.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该用户已存在");
    }
  }

  /**
   * 设置基本属性
   * @param empUserBase
   */
  private void setBase(EmpUserBase empUserBase) {
    if (StringUtils.isBlank(empUserBase.getId())){
      empUserBase.setCreateTime(new Date());
    }
    empUserBase.setModifyTime(new Date());
  }
}
